﻿@inject IUserNavigationManager unm
@* @inject IRoutingStateProvider rsp *@
@inject NavigationManager sf;
@inject IAbpSession abpSession
<div>
    <Breadcrumb @attributes=AdditionalAttributes>
        @foreach (var item in list)
        {
            <BreadcrumbItem Href="@item.Url">@item.DisplayName</BreadcrumbItem>
        }
        <BreadcrumbItem>@curr.DisplayName</BreadcrumbItem> 
    </Breadcrumb>
</div>
@code {
    [Parameter(CaptureUnmatchedValues = true)]
    public IDictionary<string, object>? AdditionalAttributes { get; set; }

    List<UserMenuItem> list = new List<UserMenuItem>();
    UserMenuItem curr = new UserMenuItem();
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        if (!abpSession.UserId.HasValue)
            return;

        _ = unm.GetMenuAsync("MainMenu", new Abp.UserIdentifier(abpSession.TenantId, abpSession.UserId.Value)).ContinueWith(tempTask=>
        {
            var menu = tempTask.Result;    
            menu.Items.RecursionDown((p, m) =>
            {
                //若后续发现rsp.RouteData.Template不行的话，考虑改用NavigationManager获取当前url匹配试试
                //https://github.com/dotnet/aspnetcore/issues/51518 rsp无法在客户端注入
                if (m.Url.IsNotNullOrWhiteSpaceBXJG() && m.Url.Equals(sf.BaseUri, StringComparison.OrdinalIgnoreCase))
                {
                    curr = m;
                    return false;
                }
                return true;
            });
            if (curr == default)
                return;
            using (var t = menu.Items.RecursionWrapParent())
            {
                curr.RecursionUp(p =>
                {
                    list.Insert(0, p);
                    return true;
                });
            }
            InvokeAsync(StateHasChanged);
        });

        

       // base.OnInitialized();
    }
}